package com.ilearn.iodata.iolingo.service.processor.core.processor;

import com.ilearn.iodata.iolingo.service.metadata.core.edm.mapper.api.GenericServiceDocument;
import com.ilearn.iodata.iolingo.service.processor.core.api.GenericODataRequestContextAccess;
import com.ilearn.iodata.iolingo.service.processor.core.api.GenericODataSessionContextAccess;
import com.ilearn.iodata.iolingo.service.processor.core.api.GenericServiceDebugger;
import com.ilearn.iodata.iolingo.service.processor.core.serializer.GenericSerializer;
import org.apache.olingo.commons.api.ex.ODataException;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.api.http.HttpHeader;
import org.apache.olingo.commons.api.http.HttpStatusCode;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.ODataResponse;
import org.apache.olingo.server.api.serializer.SerializerResult;
import org.apache.olingo.server.api.uri.UriInfo;


public abstract class GenericAbstractRequestProcessor {

  // TODO eliminate transaction handling
  protected final GenericServiceDocument sd;
  protected final GenericODataSessionContextAccess sessionContext;
  protected final UriInfo uriInfo;
  protected final GenericSerializer serializer;
  protected final OData odata;
  protected final GenericServiceDebugger debugger;
  protected int successStatusCode = HttpStatusCode.OK.getStatusCode();

  public GenericAbstractRequestProcessor(final OData odata, final GenericODataSessionContextAccess context,
                                         final GenericODataRequestContextAccess requestContext) throws ODataException {

    this.sessionContext = context;
    this.sd = context.getEdmProvider().getServiceDocument();
    this.uriInfo = requestContext.getUriInfo();
    this.serializer = requestContext.getSerializer();
    this.odata = odata;
    this.debugger = context.getDebugger();
  }

  protected final void createSuccessResponce(final ODataResponse response, final ContentType responseFormat,
      final SerializerResult serializerResult) {

    response.setContent(serializerResult.getContent());
    response.setStatusCode(successStatusCode);
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
  }
}
